<template>
  <div class="wrap">
    <div class="head">
      <common-head title="搜索结果"></common-head>
    </div>
    <div class="sort">
      <div class="order com">
        <span>综合</span>
      </div>
      <div
        @click="orderBySale"
        :class="['order', 'sale', {
        active: order.by === 'sale'
      }]"
      >
        <span>销量</span>
        <div
          :class="['up','angle',{
          active: order.by === 'sale' && order.upDown === 0
        }]"
        ></div>
        <div
          :class="['down','angle',{
          active: order.by === 'sale' && order.upDown === 1
        }]"
        ></div>
      </div>
      <div
        @click="orderByPrice"
        :class="['order','price',{
        active: order.by === 'price'
      }]"
      >
        <span>价格</span>
        <div
          :class="['up','angle',{
          active: order.by === 'price' && order.upDown === 0
        }]"
        ></div>
        <div
          :class="['down','angle',{
          active: order.by === 'price' && order.upDown === 1
        }]"
        ></div>
      </div>
      <div class="order filter">
        <span>筛选</span>
      </div>
    </div>
    <div class="recGoods">
      <div class="recommend-content" v-for="item in recGoods" :key="item.id">
            <img :src="item.pic" alt />
            <div class="recommend-right">
              <p class="name">{{item.name}}</p>
              <p class="desc">{{item.characteristic ? item.characteristic : '暂无描述' | filterDesc}}</p>
              <div class="price">
                <p>&yen;{{item.minPrice}}</p>
                <van-icon name="add-o" size="20" />
              </div>
            </div>
          </div>
    </div>
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { fetchRecGoods } from '@api'
export default {
  components: {
    CommonHead
  },
  data () {
    return {
      order: {
        by: 'sale', // sale按照销量排序，price按照价格排序
        upDown: 1 // 0代表升序，1代表降序
      },
      recGoods: []
    }
  },
  created () {
    this.fetchRecGoods()
  },
  filters: {
    filterDesc (val) {
      if (val.length > 10) {
        return val.substr(0, 10) + '...'
      } else {
        return val
      }
    }
  },
  methods: {
    // orderByCom () {
    //   if (this.order.by === 'price' || this.order.by === 'sale') {
    //     this.order.by = 'com'
    //   } else {
    //     this.order.by = 'sale'
    //   }
    // },
    fetchRecGoods (id) {
      fetchRecGoods({
        k: this.$route.query.keyword,
        orderBy: this.orderBy
      }).then(res => {
        if (res.data.code === 0) {
          this.recGoods = res.data.data.result
        }
      })
    },
    orderBySale () {
      if (this.order.by === 'price') {
        this.order.by = 'sale'
        this.order.upDown = 1
      } else {
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
      this.fetchRecGoods()
    },
    orderByPrice () {
      if (this.order.by === 'sale') {
        this.order.by = 'price'
        this.order.upDown = 0
      } else {
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
      this.fetchRecGoods()
    }
  },
  watch: {
    order: {
      handler (val) {
        this.fetchRecGoods()
      },
      deep: true
    }
  },
  computed: {
    orderBy () {
      if (this.order.by === 'sale' && this.order.upDown === 1) {
        return 'ordersDown'
      }
      if (this.order.by === 'sale' && this.order.upDown === 0) {
        return 'ordersUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 0) {
        return 'priceUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 1) {
        return 'priceDown'
      }
      return 'ordersDown'
    }
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  padding-bottom: 10px;
  .head {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
  }
  .sort {
    display: flex;
    justify-content: space-around;
    background-color: #fff;
    line-height: 42px;
    padding-top: 50px;
    .order {
      font-size: 14px;
      color: #555;
      position: relative;
      &.active {
        color: red;
      }
      .angle {
        width: 0;
        height: 0;
        border: 4px solid transparent;
        &.up {
          border-bottom-color: #000;
          position: absolute;
          top: 12px;
          left: 30px;
          &.active {
            border-bottom-color: red;
          }
        }
        &.down {
          border-top-color: #000;
          position: absolute;
          top: 22px;
          left: 30px;
          &.active {
            border-top-color: red;
          }
        }
      }
    }
  }
  .recGoods{
      .recommend-content{
      display: flex;
      margin-top: 15px;
      width: 100%;
      img{
        width: 120px;
        height: 120px;
        margin: 0 0 0 10px;
      }
      .recommend-right{
        display: flex;
        flex-direction: column;
        width: 100%;
        padding: 0 15px;
        .name{
          margin-top: 10px;
          color: rgb(13, 26, 206);
          font-size: 14px;
          font-weight: bold;
        }
        .desc{
          font-size: 12px;
          margin:30px 0 35px;
        }
        .price{
          display: flex;
          align-items: center;
          >p{
            color: #f43;
            font-weight: bold;
            flex: 1;
          }
          .van-icon-add-o{
            color: red;
          }
        }
      }
    }
  }
}
</style>
